package main

import (
	"fmt"
	"heap"
)

func main() {
	fmt.Println("-----------------Heap-----------------")

	h := heap.New() //默认是最小堆
	//h.SetComparator(revIntCmp) //设置反向比较器，变成最大堆
	arr2 := [...]int{9, 1, 5, 2, 4, 6, 3, 7, 8}
	for _, v := range arr2 {
		h.Push(v)
		fmt.Println(h)
	}
	fmt.Println("heap:", h.Empty(), h.Size())

	for !h.Empty() {
		top := h.Pop()
		fmt.Println("pop:", top, "heap:", h, "top:", h.Top())
	}
	fmt.Println("heap:", h.Empty(), h.Size())
	h.Clear()

	fmt.Println("-----------------heapify---------------")
	arr3 := make([]interface{}, 0)
	arr3 = append(arr3, 5, 1, 3, 4, 2)
	h = heap.Heapify(arr3)
	fmt.Println("heapify:", h)

	e := h.Replace(10)
	fmt.Println("replace:", e, "heap:", h)

	for !h.Empty() {
		fmt.Println("pop:", h.Pop())
	}
}

//反向int比较器，用于是实现int的最大堆
func revIntCmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(int) > b.(int) {
		return -1
	} else if a.(int32) < b.(int32) {
		return 1
	}
	return 0
}
